package com.bot.adms.controller.apply;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.bot.adms.model.vo.ApplyInfoVo;
import com.bot.adms.service.IApplyService;
import com.bot.adms.service.impl.ApplyServiceImpl;

import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 审核投放申请控制层
 * @author: 刘威林
 */
@WebServlet("/apply/check")
public class CheckApplyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");//返回数据为JSON格式

        //响应体创建
        JSONObject jsonObject = new JSONObject();
        //success定义【true成功，false失败】
        boolean success = false;
        //响应码定义【响应码,200-成功,500-失败】
        int code = 500;
        String msg = "查询失败";
        //data定义【用来返回查看的具体数据】
        JSONObject data = new JSONObject();

        try{
            //1.获取参数数据
            String idStr;
            String statusStr;

            //判断是否传入id数据
            if(req.getParameter("id") != null && !req.getParameter("id").isEmpty()){
                idStr = req.getParameter("id");
            }else{
                code = 500;
                msg = "id必须存在";
                return;
            }

            //判断是否传入状态
            if(req.getParameter("status") != null && !req.getParameter("status").isEmpty()){
                statusStr = req.getParameter("status");
            }else{
                code = 500;
                msg = "status必须存在";
                return;
            }


            //判断是否为数字
            Long id;
            int status;
            try{
                id = Long.parseLong(idStr);
                status = Integer.parseInt(statusStr);
            }catch (NumberFormatException e){
                code = 500;
                msg = "id和状态必须为数值类型";
                return;
            }

            //2.调用业务层返回数据
            IApplyService applyService = new ApplyServiceImpl();
            //检查是否符合要求
            ApplyInfoVo applyInfo = applyService.getApply(id);
            if(applyInfo == null){
                code = 500;
                msg = "未查询到该数据";
                return;
            }
            if (applyInfo.getStatus() != 2){
                code = 500;
                msg = "只有待审核状态才能审核";
                return;
            }

            //调用方法进行审核
            Boolean result = applyService.checkApply(id, status);

            //3.处理结果
            if (result == true) {
                success = result;
                code = 200;
                msg = "审核成功";

            }else{
                success = result;
                code = 500;
                msg = "审核失败";
            }

        } catch (RuntimeException e) {
            //捕获其他异常
            code = 500;
            msg = "数据异常，请稍后再试";
            e.printStackTrace();//打印日志
        }catch (Exception e) {
            //捕获其他异常
            code = 500;
            msg = "服务器异常，请稍后再试";
            e.printStackTrace();//打印日志
        }finally {
            //4组装JSON
            jsonObject.put("success", success);
            jsonObject.put("code", code);
            jsonObject.put("msg", msg);

            //将json转成字符串
            String json = JSON.toJSONString(jsonObject);

            //将json转换为字符串并返回
            ServletOutputStream out = resp.getOutputStream();
            out.write(json.getBytes("UTF-8"));
            out.flush();
            out.close();
        }


    }
}
